import json

import requests


def estest(keyword):
    url = "http://127.0.0.1:9200/lib/test/_search"
    body = {
        "query": {
            "multi_match": {
                "query": keyword,
                "fields": ["name", "key_word", "datail"]
            }
        }
    }
    headers = {
        "Content-Type": "application/json"
    }
    # data = json.dumps(body)
    resp = requests.post(url, json=body, headers=headers)
    hits = resp.json().get("hits").get("hits") if resp.json().get("hits") else None
    
    if len(hits) ==0 or hits is None:
        return None
    
    source_list = []
    for hit in hits:
        source = hit.get("_source")
        if source is None:
            continue
        source["id"] = hit.get("_id",-1)
        source_list.append(source)
    print(source_list)


if __name__ == '__main__':
    estest("的")
